package com.wanxing.sellerapp.emqx

import com.alibaba.fastjson.JSONObject
import com.wanxing.sellerapp.emqx.entity.EmqPayload
import com.wanxing.sellerapp.emqx.enums.ActionEnum
import com.wanxing.sellerapp.emqx.handlers.*
import com.wanxing.sellerapp.emqx.interfaces.IMessenger
import com.wanxing.sellerapp.seller.SellerManager

/**
 * 请求分派
 * @author xiantm
 */
class ActionDelegator(messenger:IMessenger, sellerManager: SellerManager) {


    private val actionHandlers = mapOf(
//        ActionEnum.CALIBRATE to CalibrateHandler(messenger, sellerManager),
        ActionEnum.OPEN_DOOR to OpenDoorHandler(messenger, sellerManager),
        ActionEnum.CLOSE_DOOR to CloseDoorHandler(messenger, sellerManager),
        ActionEnum.UPGRADE to UpgradeHandler(messenger, sellerManager),
        ActionEnum.DEBUG to DebugHandler(messenger, sellerManager),
        ActionEnum.DEVICE_CONFIG to ConfigHandler(messenger, sellerManager),
        ActionEnum.REBOOT to RebootHandler(messenger, sellerManager)
    )

    fun delegate(payload: EmqPayload) {
            actionHandlers[payload.action]?.handleAction(payload.messageId, payload.data?: JSONObject(), payload)
    }

}